﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.media.Sound;
    import flash.media.SoundMixer;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
	import flash.text.TextField;	
	import org.flashdevelop.utils.FlashViewer;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.cameras.Camera3D;

	/*
	 * coded by moka 2009 / 08/ 11 
	 * name : moka's SpectrumFlower
	 * blog : http://designplusplus.blogspot.com/
	 */
	
	public class Main extends Sprite 
	{		
		private var render:BasicRenderEngine;
		private var scene:Scene3D
		private var camera:Camera3D
		private var viewport:Viewport3D 
		private var ball:Sphere;
		private var bg:Bitmap;
		private var rect:Sprite;
		private var snd:Sound;
		[Embed(source="demo.mp3")]
		private var soundAsset:Class;
		
		public function Main():void 
		{			
			stage.scaleMode = "noScale";
			
			// background
			bg = addChild(new Bitmap(new BitmapData(640, 480, false, 0))) as Bitmap;
			
			// pv3d basic setting
			scene = new Scene3D();
			camera = new Camera3D();
			viewport = new Viewport3D(640, 480);
			render = new BasicRenderEngine();	
			
			// create sphere
			ball = scene.addChild(new Sphere(null, 400, 20, 15)) as Sphere;			
			
			// create sound
			snd = new soundAsset();
			snd.play(0,999);
			
			// create rect for spectrum
			rect = new Sprite();
			rect.graphics.beginFill(0xFFFFFF);
			rect.graphics.drawRect(0, 0, 1, 100);
			rect.graphics.endFill();
			
			// event registration
			stage.addEventListener(Event.ENTER_FRAME, onProcess);	
			
			// Moka's work 2009
			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>";
		}		
		
		private function onProcess(e:Event):void {
			
			// clear background
			bg.bitmapData.fillRect(bg.bitmapData.rect, 0x000000);
			
			// create spectrum texture and Material
			var bmpBuffer:BitmapData = new BitmapData(260,330,true,0);
			var bytes:ByteArray = new ByteArray();			
            SoundMixer.computeSpectrum(bytes, true, 0);
			for (var i:uint = 0; i < 256; i++){				
				if (i % 5 != 0) continue; 
				var mx:Matrix = new Matrix();
				mx.createBox(4.5, -(bytes.readFloat() * 500) / 200-0.01, 0, i, 300);
				bmpBuffer.draw(rect, mx,new ColorTransform((mouseX+256-i)/(640+256),i/256,mouseY/480/2));
			}
			
			// assign material
			ball.material = new BitmapMaterial(bmpBuffer, true);
			ball.material.doubleSided = true;
			
			// rendering
			render.renderScene(scene, camera, viewport);
			bg.bitmapData.draw(viewport);
			
			// release buffer memory
			bmpBuffer.dispose();
			
			// rotate sphere
			ball.rotationX = mouseX;
			ball.rotationY = mouseY;			
		}

	}
		
}